package cn.com.zpark.servlet_response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @PackageName: cn.com.zpark.servlet_response
 * @ClassName: ResponseDemo09
 * @Description:
 * @author: RZS
 * @date: 2021/11/14  17:44
 */
@WebServlet("/rd9.jsp")
public class ResponseDemo09 extends HttpServlet {

    private int num = 5;

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 解决响应乱码问题
        response.setContentType("text/html;charset=utf-8");
        // 获取输出字符流对象
        PrintWriter w = response.getWriter();

        w.write("<center>");

        /**
         * 实现倒计时跳转功能
         */
        // 判断num是否小于0
        if(num >= 0){
            // 倒计时

            // 设置页面定时刷新，间隔1秒一次,设置响应头字段（refresh）实现页面的定时跳转
            response.setHeader("refresh", "1");
            // 响应信息
            w.write("<h1>"+num+"秒之后跳转到登录页面</h1>");
            // num--
            num--;
        }else{
            // 跳转至登录页面
            // 重定向到登录页面
            response.sendRedirect(getServletContext().getContextPath()+"/view/index.html");
        }

        w.write("</center>");
    }
}

